package org.dna.opdracht4;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

public class Apl {
	private PERTNetwork<Character> pert;
	
	public Apl() {
		pert = new PERTNetwork<Character>();
		
		readFile("network1.txt");
		
		List<Vertex<Character>> list = pert.topologicalSort();
		
		
		System.out.println("== Topological Sorted");
		for(Vertex<Character> vert: list){
			System.out.println(vert);
		}
		
		System.out.println("\n== Topological Reversed");
		for(Vertex<Character> vert: pert.topologicalSortReversed(list)){
			System.out.println(vert);
		}
		
		for(Vertex<Character> vertex : pert.getVertices()) {
			System.out.println(vertex);

			for(Edge<Character> edge : vertex.getInEdges()) {
				System.out.println(edge);
			}
		}
	}
	
	private void readFile(String filename) {
		String currentType = "";
		
		try {
			BufferedReader input =  new BufferedReader(new FileReader(filename));

			try {
				String line = null;

				while ((line = input.readLine()) != null) {
					if(line.equals("[vertex]")) {
						currentType = "vertex";
						continue;
					} else if(line.equals("[edge]")) {
						currentType = "edge";
						continue;
					}
					
					if(currentType.equals("vertex")) {
						String[] vertex = line.split(":");
						
						pert.addVertex(new Vertex<Character>(vertex[0].charAt(0), Integer.valueOf(vertex[1]), Integer.MAX_VALUE));
					} else if(currentType.equals("edge")) {
						String[] edge = line.split(":");
						
						Vertex<Character> from = pert.findVertex(edge[0].charAt(0));
						Vertex<Character> to = pert.findVertex(edge[1].charAt(0));
						
						Edge<Character> edgev = new Edge<Character>(from, to, Integer.valueOf(edge[2]));
						from.addOutEdge(edgev);
						to.addInEdge(edgev);
					}
				}
			} catch(NumberFormatException e) {
				e.printStackTrace();
			} finally {
				input.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] arg) {
		new Apl();
	}
}
